home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Programming Contest / ~Solutions Submitted / Problem 10 - McMullen / Solution.p < prev   
Encoding:
Text File  |  1998-06-19  |  2.3 KB  |  77 lines  |  [TEXT/CWIE]

  1. (*
  2. Problem 10 - Interpreter
  3.  
  4. Write an interpreter for this simple 32-bit integer language with 26 registers
  5. labeled A-Z.
  6.  
  7. type RegisterArray = array[0..25] of SInt32;
  8.  
  9. procedure Interpret( source: Handle; var result: RegisterArray );
  10.  
  11. source consists of a number of lines of the form:
  12.  
  13. [<label>:][<tab><instruction><tab><operand-list>]<cr>
  14.  
  15. <label> is a sequence of at least two alphanumerics (0-9a-zA-Z_)
  16. (casesensitive).
  17. <operand-list> is a sequence of operators seperated by commas
  18. other than the tabs and crs, no white space is allowed.
  19.  
  20. <instruction> (and appropriate operands are:
  21.  
  22. MOVE    <value>,<register>            (set <register> to <value>)
  23. ADD     <value1>,<value2>,<register>  (set <register> to <value1>+<value2>)
  24. SUB     <value1>,<value2>,<register>  (set <register> to <value1>-<value2>)
  25. JMP     <label>                       (jump to line labeled with <label>)
  26. JEQ     <value1>,<value2>,<label>     (jump to <label> if <value1> = <value2>)
  27. JLE     <value1>,<value2>,<label>     (jump to <label> if <value1> <= <value2>)
  28. JSR     <label>                       (jump to subroutine at <label>)
  29. RTS                                   (return from subroutine)
  30. PUSH    <value>                       (push value on to stack)
  31. POP     <register>                    (pop value from stack)
  32.  
  33. <register> is a single uppercase letter in the range A..Z
  34. <number> is an optional minus sign followed by decimal digits
  35. <value> is a <register> or a <number>
  36.  
  37. Note that the data stack and subroutine stack are independent stacks.  A RTS is
  38. used to stop the program (that is, consider that you have JSRed to the initial
  39. line of the source handle).  For example:
  40.  
  41.     PUSH    10
  42.     JSR    setA
  43.     JSE setB
  44.     RTS
  45. setA:
  46.     POP    A
  47.     RTS
  48. setB:    MOVE    A,B
  49.     RTS
  50.     
  51. Interpret takes the source handle and JSRs to it, presetting the registers
  52. according to the register array.  When the code returns, the register array is
  53. set to the final value of all the registers.  Both subroutine and data stacks
  54. should be large (at least 1000 entries each).
  55. *)
  56.  
  57. unit Solution;
  58.  
  59. interface
  60.  
  61. // Do not modify the interface
  62.  
  63.     uses
  64.         Types, Files;
  65.         
  66. type RegisterArray = array[0..25] of SInt32;
  67.  
  68. procedure Interpret( source: Handle; var result: RegisterArray );
  69.  
  70. implementation
  71.  
  72. // Fill in your solution and then submit this folder
  73.  
  74. // Team Name: FILL IN YOUR TEAM NAME!
  75.  
  76. end.
  77.